package com.yc.boottxx.controller;

import com.yc.boottxx.service.BizException;
import com.yc.boottxx.vo.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@Component
@RestControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(BizException.class)
    Result bizException(BizException e) {
        return Result.fail(e.getMessage(), e.getData());
    }

    // 拦截 除BizException 以外的异常
    @ExceptionHandler(Exception.class)
    Result exception(Exception e){
        e.printStackTrace();
        return Result.fail("业务繁忙,请稍后再试");
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    Result maxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        System.out.println("文件上传最大大小 e.getMaxUploadSize() = " + e.getMaxUploadSize());
        return Result.fail("上传的文件大小不能超过 1 MB!");
    }

}
